package com.neuedu.javase.collection;

import com.sun.source.util.Trees;

import java.util.*;

/***
 * 项目名称:  MyJavaSE
 * 包：  com.neuedu.javase.collection
 * date:   2025/11/5   8:55
 * 描述:
 *  @author : 张金山
 *  作者博客:   https://blog.fulfill.com.cn
 */
public class MyHashSet {


    public static void main(String[] args) {


        //构造对象  面相接口编程
        // HashSet set = new HashSet();
        HashSet set = new HashSet(32);

        //元素的添加
        set.add("张三");
        set.add("李四");
        set.add("王五");
        set.add("张三");


        System.out.println("set.size() = " + set.size());


        Set set2 = new HashSet(set);
        System.out.println("set2.size() = " + set2.size());


        System.out.println("=======================删除元素=============================");
        /**
         * //删除指定的某个元素。
         * public　boolean　remove(Object　o)
         *
         * //删除指定的多个元素。
         * public　boolean　removeAll(Collection　c)
         *
         * //删除所有的元素。
         * public　void　clear()
         *
         * //只保留指定集合中存在的元素，其他的都删除，相当于取两个集合的交集。
         * public　boolean　retainAll(Collection　c)
         */

        HashSet set3 = new HashSet(Arrays.asList("张三","李四","王五","赵六"));

        System.out.println("set3 = " + set3);
        set3.remove("张三");
        System.out.println("set3.remove(\"张三\") = " + set3);


        set3.removeAll(Arrays.asList("王五","赵六","宋四"));
        System.out.println("set3.removeAll = " + set3);


        HashSet set4 = new HashSet(Arrays.asList("张三","李四","王五","赵六"));
        System.out.println("set4 = " + set4);
        set4.retainAll(Arrays.asList("张三","李四"));
        System.out.println("set4  retainAll = " + set4);

        System.out.println("set4.contains(\"张三\") = " + set4.contains("张三"));  //true
        System.out.println("set4.contains(\"赵六\") = " + set4.contains("赵六"));  //false

        set4.clear();
        System.out.println("set4 clear  = " + set4);  //【】
        System.out.println("set4.isEmpty() = " + set4.isEmpty());  //true


        System.out.println("=======================遍历=============================");
        //Set 集合的遍历

        Set  humanSet = new HashSet();
        humanSet.add(new Human("张三",18));
        humanSet.add(new Human("张三1",28));
        humanSet.add(new Human("张三2",38));
        humanSet.add(new Human("张三3",48));
        humanSet.add(new Human("张三4",58));

        // humanSet.add(100);

        // Iterator it = humanSet.iterator();
        // while (it.hasNext()){
        //     System.out.println("while it.next() = " + it.next());
        // }



        // for (   Iterator it = humanSet.iterator()    ;  it.hasNext()        ;     ){
        //     Human human = (Human) it.next();
        //     System.out.println("for it.next()   = " + human);
        // }


        // humanSet.for
        // for (  Object obj     :   humanSet   ){
        //     Human human = (Human) obj;
        //         System.out.println("增强 for（JDK1.5+）    = " + human);
        // }


        System.out.println("=====1.8+  humanSet.forEach( System.out::println  )====");
        // 1.8+
        // humanSet.stream().forEach();
        humanSet.forEach( System.out::println  );



    }




}



